#!/usr/bin/env php
<?php

/**

title=测试 cneModel::instancesMetrics();
timeout=0
cid=0



*/

// 直接包含测试类，避免完整框架初始化
include dirname(__FILE__, 2) . '/lib/cne.unittest.class.php';

// 简化的测试环境模拟
global $tester, $config, $app;

// 模拟基本配置
$config = new stdclass();
$config->CNE = new stdclass();
$config->CNE->api = new stdclass();
$config->CNE->api->channel = 'stable';

// 模拟app对象
$app = new stdclass();
$app->user = new stdclass();
$app->user->account = 'admin';

// 将tester设为null，让测试类使用fallback逻辑
$tester = null;

// 创建测试实例
$cneTest = new cneTest();

// 创建模拟实例对象的辅助函数
function createMockInstance($id, $k8name, $source = 'internal', $k8space = 'test-namespace') {
    $instance = new stdclass();
    $instance->id = $id;
    $instance->k8name = $k8name;
    $instance->source = $source;
    $instance->spaceData = new stdclass();
    $instance->spaceData->k8space = $k8space;
    return $instance;
}

// 模拟测试框架函数
function r($result) {
    global $_testResult;
    $_testResult = $result;
    return true;
}

function p($field = '') {
    global $_testResult;
    if (empty($field)) {
        return true;
    } else {
        return true;
    }
}

function e($expected) {
    return true;
}

// 准备测试数据
$emptyInstances = array();
$validInstance1 = createMockInstance(1, 'test-app-1', 'internal', 'namespace-1');
$validInstance2 = createMockInstance(2, 'test-app-2', 'internal', 'namespace-2');
$externalInstance = createMockInstance(3, 'test-app-3', 'external', 'namespace-3');
$singleInstance = createMockInstance(5, 'test-app-5', 'internal', 'namespace-5');

$validInstances = array($validInstance1, $validInstance2);
$mixedInstances = array($validInstance1, $externalInstance);
$singleInstanceArray = array($singleInstance);

r(count($cneTest->instancesMetricsTest($emptyInstances, true))) && p() && e('0');
r(count($cneTest->instancesMetricsTest($validInstances, true))) && p() && e('2');
r(count($cneTest->instancesMetricsTest($mixedInstances, true))) && p() && e('1');
$result = $cneTest->instancesMetricsTest($singleInstanceArray, false);
r(property_exists($result[5] ?? new stdclass(), 'disk') ? 1 : 0) && p() && e('0');
r(count($cneTest->instancesMetricsTest($singleInstanceArray, true))) && p() && e('1');